<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
       <el-form-item label="联系方式" prop="phone">
         <el-input
             v-model="queryParams.phone"
             placeholder="请输入联系方式"
             clearable
             style="width: 200px"
             @keyup.enter="handleQuery"
         />
       </el-form-item>
       <el-form-item label="考证培训状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="考证培训状态" filterable clearable style="width: 200px">
          <el-option
            v-for="dict in dict.type.pilot_train_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="联系人名称" align="center" prop="userName"/>
      <el-table-column label="联系方式" align="center" prop="phone"/>
      <el-table-column label="期望培训时间" align="center" prop="expectedTrainingTime"/>
      <el-table-column label="培训开始时间" align="center" prop="trainingTimeBegin"/>
      <el-table-column label="培训结束时间" align="center" prop="trainingTimeEnd"/>
      <el-table-column label="考证培训状态" align="center" prop="status">
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.pilot_train_status" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="text" icon="el-icon-view" @click="handleDetail(row)"
            v-hasPermi="['member:certificateTrainingObtain:query']">查看详情
          </el-button>
          <el-button type="text" @click="handleAuth(row)" v-if="row.status == 2"
            v-hasPermi="['member:certificateTrainingObtain:edit']">开始培训
          </el-button>
          <el-button type="text" @click="handleAuth(row)" v-if="row.status == 3"
            v-hasPermi="['member:certificateTrainingObtain:edit']">结束培训
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改计费规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row class="diy-text-sm diy-leading-10" :gutter="10" v-if="title == '查看详情'">
          <el-col :span="item.span || 12" v-for="(item,index) in formDetail" :key="index">
            <div class="flex_hcneter">
              <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
              <div v-if="item.zidian && item.isCustom" class="flex_hcneter">
                {{item.label}}：<dict-tag :options="dict.type[item.zidian]" :value="form[item.prop]"/>
              </div>
              <div v-else-if="item.prop == 'content' && item.isCustom">
                {{item.label}}：
                <div v-html="form[item.prop]"></div>
              </div>
              <div v-else-if="item.type == 'image' && item.isCustom" class="flex_hcneter" style="margin-bottom:10px;">
                {{item.label}}：
                <el-image
                  style="width: 100px; height: 100px;"
                  :src="form[item.prop]"
                  :preview-src-list="[form[item.prop]]">
                </el-image>
              </div>
              <div v-else>{{item.label}}：{{ form[item.prop] }}{{ item.danwei || '' }}</div>
            </div>
          </el-col>
        </el-row>
        <div v-else>
          <el-form-item label="选择时间" prop="trainingTime">
            <el-date-picker
              v-model="form.trainingTime"
              value-format="yyyy-MM-dd"
              type="date">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="训练基地" prop="trainingSiteId">
            <el-select v-model="form.trainingSiteId" clearable filterable placeholder="请选择训练基地">
              <el-option
                v-for="item in trainList"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="证书编号" prop="certificateCode" v-if="title == '结束培训'">
            <el-input v-model="form.certificateCode" placeholder="请输入证书编号"/>
          </el-form-item>
          <el-form-item label="有效期" prop="validityPeriod" v-if="title == '结束培训'">
            <el-input v-model="form.validityPeriod" placeholder="请输入有效期"/>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="title != '查看详情'" :loading="popLodding" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/pilot/flightAttenTrainCert";
import { listAllCertificateTrainingSite, } from "@/api/pilot/certifTrainDest";
export default {
name: "FlightAttenTrainCert",
dicts: ['pilot_train_status',],
components: {  },
data() {
  return {
    popLodding:false,//弹窗加载
    formDetail:[//表单
      {label:'联系人名称',prop:'userName',},{label:'联系方式',prop:'phone',},{label:'期望培训时间',prop:'expectedTrainingTime',},
      {label:'培训开始时间',prop:'trainingTimeBegin',},{label:'培训结束时间',prop:'trainingTimeEnd',},{label:'省',prop:'userProvinceName',},
      {label:'市',prop:'userCityName',},{label:'区',prop:'userCountyName',},
      {label:'证件类型名称',prop:'certificateTypeName',},{label:'训练基地',prop:'trainingSiteName',},
      {label:'考证培训状态',prop:'status',zidian:'pilot_train_status',isCustom:true,},
    ],
    form:{},
    // 弹出层标题
    title: "",
    // 是否显示弹出层
    open: false,
    // 遮罩层
    loading: true,
    // 显示搜索条件
    showSearch: true,
    queryParams: {// 查询参数
      pageNum: 1,phone:null,status:null,
      pageSize: 10,
    },
    // 总条数
    total: 0,
    rules: {
      trainingTime: [{required: true, message: "选择时间不能为空", trigger: "change"}],
      trainingSiteId: [ { required: true, message: "训练基地不能为空", trigger: "change" } ],
      certificateCode: [ { required: true, message: "证书编号不能为空", trigger: "blur" } ],
      validityPeriod: [ { required: true, message: "有效期不能为空", trigger: "blur" } ],
    },
    list:[],//页面数据
    trainList:[],//训练基地
  };
},
created() {
  this.getList()
},
methods: {
  async listAllCertificateTrainingSite() {// 训练基地列表
    let resp = await listAllCertificateTrainingSite()
    this.trainList = resp.data || []
  },
  getList() {
    this.loading = true;
    api.listCertificateTrainingObtain(this.queryParams).then(response => {
      this.list = response.rows || [];
      this.total = response.total || 0;
      this.loading = false;
    });
  },
  handleAuth(row){//审核
    this.reset();
    this.open = true;
    let status = row.status
    this.title = status == 3 ? '结束培训' : "开始培训";
    this.form = {
      id:row.id,status,trainingSiteId:row.trainingSiteId,
    }
  },
  handleDetail(row) {//查看详情
    this.reset();
    const id = row.id || this.ids;
    this.open = true;
    this.title = "查看详情";
    api.getCertificateTrainingObtain(id).then(response => {
      let data = response.data || {}
      this.form = {
        ...data,
      };
    });
  },
  submitForm() {
    this.$refs["formRef"].validate(valid => {
      if (valid) {
        let form = {...this.form}
        let apis = form.status == 3 ? api.toEndTraining : api.toBeginTraining
        this.popLodding = true
        apis(form).then(response => {
          this.popLodding = false
          this.$modal.msgSuccess("操作成功")
          this.open = false;
          this.getList();
        }).catch(error=>this.popLodding = false);
      }
    });
  },
  // 表单重置
  reset() {
    this.form = {};
    this.resetForm("formRef");
    this.listAllCertificateTrainingSite();
  },
  /** 搜索按钮操作 */
  handleQuery() {
    this.queryParams.pageNum = 1;
    this.getList();
  },
  /** 重置按钮操作 */
  resetQuery() {
    this.resetForm("queryForm");
    this.handleQuery();
  },
}
};
</script>
